package handler

import (
	"encoding/json"
	"github.com/labstack/echo/v4"
	"log"
	"strconv"
	"time"
	"ulcdkey/db"
	"ulcdkey/tool"
)

// 获取cdk类列表
func GetCdkTypeListByGameId(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)
	cdkTypes := new(db.CdkType)

	gameIdStr := c.FormValue("gameId")
	pageStr := c.FormValue("page")
	limitStr := c.FormValue("limit")

	cdkTypes.GameId, err = strconv.Atoi(gameIdStr)
	if err != nil {
		log.Println("gameId参数错误 err:", err)
		r.Code = -1
		r.Msg = "gameId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	page, err := strconv.Atoi(pageStr)
	if err != nil {
		page = 1
	}

	limit, err := strconv.Atoi(limitStr)
	if err != nil {
		limit = 50
	}

	as, counts, err := cdkTypes.GetCdkTypeListByGameId(page, limit)
	if err != nil {
		log.Println("GetCdkType err:", err)
		r.Code = -1
		r.Msg = "获取cdk类列表失败,err=" + err.Error()
		return c.JSON(200, r)
	}

	r.Data = map[string]interface{}{
		"results": as,
		"counts":  counts,
	}

	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 增加cdk类
func AddCdkType(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)
	cdkTypes := new(db.CdkType)
	userInfo := tool.GetVisitUserInfo(c)

	cdkTypes.Name = c.FormValue("name")
	cdkTypes.StartDate = c.FormValue("startDate")
	cdkTypes.EndDate = c.FormValue("endDate")
	cdkTypes.Desc = c.FormValue("desc")
	channelInfo := c.FormValue("channelId")

	gameIdStr := c.FormValue("gameId")

	totalCountStr := c.FormValue("totalCount")
	userLimitCountStr := c.FormValue("userLimitCount")
	bUseTimesStr := c.FormValue("bUseTimes")
	rewardListStr := c.FormValue("rewardList")

	endTime, err := time.ParseInLocation("2006-01-02 15:04:05", cdkTypes.EndDate, time.Local)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "endDate参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	if time.Now().After(endTime) {
		r.Code = -1
		r.Msg = "有效期结束时间应大于当前时间"
		return c.JSON(200, r)
	}

	cdkTypes.GameId, err = strconv.Atoi(gameIdStr)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "gameId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = json.Unmarshal([]byte(channelInfo), &cdkTypes.ChannelId)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "channelId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	cdkTypes.TotalCount, err = strconv.Atoi(totalCountStr)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "totalCount参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}
	cdkTypes.UserLimitCount, err = strconv.Atoi(userLimitCountStr)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "userLimitCount参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}
	cdkTypes.BUseTimes, err = strconv.Atoi(bUseTimesStr)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "bUseTimes参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = json.Unmarshal([]byte(rewardListStr), &cdkTypes.RewardList)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "rewardList参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = cdkTypes.AddCdkType(userInfo)
	if err != nil {
		log.Println("AddCdkType err:", err)
		r.Code = -1
		r.Msg = "新增cdk类失败,err=" + err.Error()
		return c.JSON(200, r)
	}

	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 更新cdk类
func UpdateCdkType(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)
	cdkTypes := new(db.CdkType)
	userInfo := tool.GetVisitUserInfo(c)

	cdkTypes.Name = c.FormValue("name")
	cdkTypes.StartDate = c.FormValue("startDate")
	cdkTypes.EndDate = c.FormValue("endDate")
	cdkTypes.Desc = c.FormValue("desc")
	channelInfo := c.FormValue("channelId")

	cdkTypeIdStr := c.FormValue("id")
	gameIdStr := c.FormValue("gameId")
	totalCountStr := c.FormValue("totalCount")
	userLimitCountStr := c.FormValue("userLimitCount")
	bUseTimesStr := c.FormValue("bUseTimes")
	rewardListStr := c.FormValue("rewardList")

	cdkTypes.Id, err = strconv.Atoi(cdkTypeIdStr)
	if err != nil {
		log.Println("UpdateCdkType err:", err)
		r.Code = -1
		r.Msg = "id参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	cdkTypes.GameId, err = strconv.Atoi(gameIdStr)
	if err != nil {
		log.Println("UpdateCdkType err:", err)
		r.Code = -1
		r.Msg = "gameId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = json.Unmarshal([]byte(channelInfo), &cdkTypes.ChannelId)
	if err != nil {
		log.Println("GetCdkType err:", err)
		r.Code = -1
		r.Msg = "channelId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	cdkTypes.TotalCount, err = strconv.Atoi(totalCountStr)
	if err != nil {
		log.Println("UpdateCdkType err:", err)
		r.Code = -1
		r.Msg = "totalCount参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	cdkTypes.UserLimitCount, err = strconv.Atoi(userLimitCountStr)
	if err != nil {
		log.Println("UpdateCdkType err:", err)
		r.Code = -1
		r.Msg = "userLimitCount参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}
	cdkTypes.BUseTimes, err = strconv.Atoi(bUseTimesStr)
	if err != nil {
		log.Println("UpdateCdkType err:", err)
		r.Code = -1
		r.Msg = "bUseTimes参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = json.Unmarshal([]byte(rewardListStr), &cdkTypes.RewardList)
	if err != nil {
		log.Println("UpdateCdkType err:", err)
		r.Code = -1
		r.Msg = "rewardList参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = cdkTypes.UpdateCdkType(userInfo)
	if err != nil {
		log.Println("UpdateCdkType err:", err)
		r.Code = -1
		r.Msg = "更新cdk类失败,err=" + err.Error()
		return c.JSON(200, r)
	}

	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 删除cdk类
func DelCdkType(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)
	cdkTypes := new(db.CdkType)
	userInfo := tool.GetVisitUserInfo(c)

	cdkTypeIdStr := c.FormValue("id")

	cdkTypes.Id, err = strconv.Atoi(cdkTypeIdStr)
	if err != nil {
		log.Println("DelCdkType err:", err)
		r.Code = -1
		r.Msg = "id参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = cdkTypes.DelCdkType(userInfo)
	if err != nil {
		log.Println("DelCdkType err:", err)
		r.Code = -1
		r.Msg = "删除cdk类失败,err=" + err.Error()
		return c.JSON(200, r)
	}

	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}
